package DailyPractice.Day7;

import sun.plugin.javascript.navig.Link;

import java.util.*;

/**
 * @author lumin
 * @date 2022/04/25 11:28
 **/
public class Parenthesis {
    public static boolean chkParenthesis(String A, int n) {
        if (n % 2 == 1) {
            return false;
        }
        Map<Character, Character> pairs = new HashMap<Character, Character>() {{
            put(')', '(');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
            char ch = A.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String str = "()(()()";
        System.out.println(chkParenthesis(str, 7));
    }
}